//
//  ZLPSubjectTableViewCell.m
//  TangYi
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 赵连鹏. All rights reserved.
//

#import "ZLPSubjectTableViewCell.h"
#import "ZLPRankModel.h"
#import <UIImageView+WebCache.h>
#import "ZLPRankCollectionViewCell.h"


@implementation ZLPSubjectTableViewCell

- (void)dealloc {
    
    [_descLabel release];
    [_collectView release];
    [_rankLabel release];
    [_rankArr release];
    
    [super dealloc];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    self.descLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.descLabel];
    [_descLabel release];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(50, 50);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    [self.contentView addSubview:self.collectView];
    self.collectView.backgroundColor = [UIColor whiteColor];
    [_collectView release];
    
    self.collectView.showsHorizontalScrollIndicator = NO;
    
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    
    [self.collectView registerClass:[ZLPRankCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    
    self.rankLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.rankLabel];
    self.rankLabel.textAlignment = NSTextAlignmentCenter;
    [_rankLabel release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZLPRankCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    if (indexPath.row < 5) {
        ZLPRankModel *model = self.rankArr[indexPath.row];
//        NSLog(@"%@", model.avatar);
        [cell.rankImage sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    } else {
        cell.rankImage.image = [UIImage imageNamed:@"right.png"];
//        cell.backgroundColor = [UIColor colorWithRed:0.885 green:0.695 blue:0.525 alpha:1.000];
    }
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.delegate pushCollentionView:self.rankArr];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    
    CGRect rect = [self.descLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.descLabel.frame = CGRectMake(5, 0, self.contentView.frame.size.width - 10, rect.size.height + 20);
    self.descLabel.font = [UIFont systemFontOfSize:16];
    
    self.rankLabel.frame = CGRectMake(5, rect.size.height + 10, self.contentView.frame.size.width - 10, 30);
    
    self.collectView.frame = CGRectMake(5, rect.size.height + 38, self.contentView.frame.size.width - 10, 50);
    
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
